# -*- python -*-
# ex: set filetype=python:

from buildbot.plugins import *
import itertools

# This is a sample buildmaster config file. It must be installed as
# 'master.cfg' in your buildmaster's base directory.

# This is the dictionary that the buildmaster pays attention to. We also use
# a shorter alias to save typing.
c = BuildmasterConfig = {}

qe_project = 'https://gitlab.com/QEF/q-e.git'
projects = ['https://gitlab.com/QEF/q-e.git']

worker_builders = { "farmer-worker1": ["farmer_intel19_mvapich23","farmer_gcc102_openmpi404","farmer_gcc750_openmpi316-dbg",\
            "farmer_pgi1910_openmpi313","farmer_gcc102_serial","farmer_gcc102_openmpi404_openmp",\
            "farmer_gcc102_openmpi404_hdf5","farmer_intel20_impi","farmer_pgi1910_GPU","farmer_gcc102_openmpi404_libxc"]}

workers = worker_builders.keys()
builders = list( itertools.chain.from_iterable( worker_builders.values() ) )
print(builders)

def getworkerbybuilders(b):
        for s in workers:
                for i in range(len(worker_builders[s])):
                        if b == worker_builders[s][i]:
                                return s
        return None


builder_epw = []
builder_wan = []
for v in builders:
  builder_epw.extend(["%s-%s" % ('QE', v)])
  builder_wan.extend(["%s-%s" % ('WAN', v)])

builder_qe = builder_epw 
builder_all = builder_epw + builder_wan #+ builder_test

####### WORKERS

# The 'workers' list defines the set of recognized workers. Each element is
# a Worker object, specifying a unique worker name and password.  The same
# worker name and password must be configured on the worker.
c['workers'] = [worker.Worker("farmer-worker1", "uzImI36q", max_builds=2)]

# 'protocols' contains information about protocols which master will use for
# communicating with workers. You must define at least 'port' option that workers
# could connect to your master with this protocol.
# 'port' must match the value configured into the workers (with their
# --master option)
c['protocols'] = {'pb': {'port': 9989}}

####### CHANGESOURCES

# the 'change_source' setting tells the buildmaster how it should find out
# about source code changes.  Here we point to the buildbot version of a python hello-world project.

from buildbot.changes.gitpoller import GitPoller

c['change_source'] = []
###c['change_source'].append(changes.GitPoller(
###        'git://github.com/buildbot/hello-world.git',
###        workdir='gitpoller-workdir', branch='master',
###        pollInterval=300))
c['change_source'].extend([
        GitPoller(
            project='quantum_espresso',
#            repourl='https://github.com/QEF/q-e.git',
            repourl='https://gitlab.com/QEF/q-e.git',
            branches=['develop'],
            workdir='gitpoller_QE',
            category='qe_update',
        )])
c['change_source'].extend([
        GitPoller(
            project='wannier90',
            repourl='https://github.com/wannier-developers/wannier90.git',
            branches=['develop'],
            workdir='gitpoller_WAN',
            category='wan_update',
        )])

####### SCHEDULERS

# Configure the Schedulers, which decide how to react to incoming changes.  In this
# case, just kick off a 'runtests' build

from buildbot.changes.filter import ChangeFilter
from buildbot.schedulers.timed import Nightly

c['schedulers'] = []
c['schedulers'].extend([
        Nightly(
          name = 'QE' + '-nightly',
          change_filter=ChangeFilter(
              project = 'quantum_espresso',
              category = 'qe_update',
              branch = ['develop'],
          ),
          onlyIfChanged = True,
          hour = 0,
          minute = 0,
          builderNames=builder_qe,
          createAbsoluteSourceStamps = True,
        )
])
c['schedulers'].extend([
        Nightly(
          name = 'WAN' + '-nightly',
          change_filter=ChangeFilter(
              project = 'wannier90',
              category = 'wan_update',
              branch = 'develop',
          ),
          branch = 'develop',
          onlyIfChanged = True,
          hour = 0,
          minute = 0,
          builderNames=builder_wan,
          createAbsoluteSourceStamps = True,
        )
])

from buildbot.schedulers.forcesched import ForceScheduler

c['schedulers'].extend([
  ForceScheduler(
    name='force',
    builderNames=builder_all,
    reasonString='%(reason)s',
    codebases=[
      util.CodebaseParameter(
        "",
        name='branch',
        branch=util.ChoiceStringParameter(
            name="branch",
            choices=["develop"],
            default="develop"),
        # Remove the fields 
        revision=util.FixedParameter(name="revision", default=""),
        repository=util.FixedParameter(name="repository", default=""),
        project=util.FixedParameter(name="project", default=""),
      )
    ],
  )])



###c['schedulers'].append(schedulers.SingleBranchScheduler(
###                            name="all",
###                            change_filter=util.ChangeFilter(branch='master'),
###                            treeStableTimer=None,
###                            builderNames=["runtests"]))
###c['schedulers'].append(schedulers.ForceScheduler(
###                            name="force",
###                            builderNames=["runtests"]))

####### BUILDERS

# The 'builders' list defines the Builders, which tell Buildbot how to perform a build:
# what steps, and which workers can execute them.  Note that any particular build will
# only take place on one worker.

from buildbot.config import BuilderConfig
from buildbot.plugins import steps
from buildbot.process.factory import BuildFactory
from buildbot.steps.shell import ShellCommand
from buildbot.steps.shell import Compile

c['builders'] = []
for BuilderID in builders:
      print('BuilderID ',BuilderID)
      exec(open('%s.cfg' % BuilderID).read())
      c['builders'].append(
              BuilderConfig(
                      name= '%s-%s' % ('QE',BuilderID), 
                      workernames=getworkerbybuilders(BuilderID),
                      factory=f,
              )
      )
for BuilderID in builders:
      exec(open('%s.cfg' % BuilderID).read())
      c['builders'].append(
              BuilderConfig(
                      name= '%s-%s' % ('WAN',BuilderID), 
                      workernames=getworkerbybuilders(BuilderID),
                      factory=f_WAN,
              )
      )


####### BUILDBOT SERVICES

# 'services' is a list of BuildbotService items like reporter targets. The
# status of each build will be pushed to these targets. buildbot/reporters/*.py
# has a variety to choose from, like IRC bots.

email = ['samuel.pon@gmail.com','martin.schlipf@gmail.com','p.giannozzi@gmail.com',\
         'degironc@sissa.it','baroni@sissa.it','feliciano.giustino@materials.ox.ac.uk',\
         'pdelugas@sissa.it','p.bonfa@cineca.it','andrea.ferretti@nano.cnr.it',\
         'dalcorso@sissa.it','brunato@sissa.it','icarnimeo@sissa.it',\
         'lercole@sissa.it','fgrassel@sissa.it','iurii.timrov@epfl.ch',\
         'c.cavazzoni@cineca.it','akaithal@sissa.it' ]

c['services'] = []
mn = reporters.MailNotifier(fromaddr="testfarmqef@gmail.com",
                            sendToInterestedUsers=True,
                            extraRecipients=email,
                            relayhost="smtp.gmail.com",
                            smtpUser="testfarmqef@gmail.com",
                            mode=('failing'),
                            useSmtps=True,
                            smtpPort=465, 
                            builders=builder_epw, 
                            #useTls=True, 
                            subject='Test-farm failing',
                            smtpPassword="XXXX")
c['services'].append(mn)

####### PROJECT IDENTITY

# the 'title' string will appear at the top of this buildbot installation's
# home pages (linked to the 'titleURL').

c['title'] = "QEF_FARM"
c['titleURL'] = "http://foundation.quantum-espresso.org/"

# the 'buildbotURL' string should point to the location where the buildbot's
# internal web server is visible. This typically uses the port number set in
# the 'www' entry below, but with an externally-visible host name which the
# buildbot cannot figure out without some help.

c['buildbotURL'] = "http://158.110.145.67:8010/"

# minimalistic config to activate new web UI
#c['www'] = dict(port=8010,
#                plugins=dict(waterfall_view={}, console_view={}, grid_view={}))

c['www'] = dict(port=8010,
                plugins=dict(waterfall_view={},
                console_view={}),
                auth=util.UserPasswordAuth({"admin": "XXXX"}) )

####### DB URL

c['db'] = {
    # This specifies what database buildbot uses to store its state.
    # It's easy to start with sqlite, but it's recommended to switch to a dedicated
    # database, such as PostgreSQL or MySQL, for use in production environments.
    # http://docs.buildbot.net/current/manual/configuration/global.html#database-specification
    'db_url' : "sqlite:///state.sqlite",
}
